package com.bw.login.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.view.animation.LinearInterpolator
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.login.databinding.ActivityRegister2Binding
import com.bw.login.databinding.ActivityRegisterBinding
import com.bw.login.net.LoginViewModel
import kotlin.random.Random

@Route(path = RoutePath.PATH_REGISTER)

class RegisterActivity : BaseMvvmActivity<ActivityRegister2Binding, LoginViewModel>() {

    private lateinit var mValueAnimator: ValueAnimator
    private var mValue = 0


    override fun initView() {
        mValueAnimator = ValueAnimator.ofInt(5, 0)



        binding.CheckCode.setOnClickListener {
            getPhoneCode()


        }


        binding.callback.setOnClickListener {
            finish()
        }

        binding.register.setOnClickListener {
            val username = binding.username.text.toString();
            val password = binding.password.text.toString();
            val rePassword = binding.prePassword.text.toString();
            val code = binding.code.text.toString();

            if (!password.equals(rePassword)) {
                ToastUtils.showShort("两次密码不一致")
                return@setOnClickListener
            }


            if (username.equals("") || password.equals("")) {
                ToastUtils.showShort("请先输入账号和密码")
                return@setOnClickListener
            }
            if (code.equals("")) {
                ToastUtils.showShort("请先输入验证码")
                return@setOnClickListener
            }


            vm.register(username, password);
        }
    }

    override fun initData() {
        vm.registerSuccess.observe(this) {
            ToastUtils.showShort("注册成功" + it.ctime)
            finish()
        }

        vm.onFailed.observe(this) {
            ToastUtils.showShort("注册失败" + it.message)
        }
    }

    private fun getPhoneCode() {
        mValueAnimator = ValueAnimator.ofInt(5, 0)
        mValueAnimator.duration = 5000
        mValueAnimator.interpolator = LinearInterpolator()
        binding.CheckCode.isEnabled = false

        mValueAnimator.addUpdateListener {
            val count = it.animatedValue as Int
            if (count != mValue) {
                mValue = count
                binding.CheckCode.text = "${count}秒后重新发送"
            }
        }

        mValueAnimator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                //随机数模拟验证码
                val toInt = Random.nextDouble(100000.00, 1000000.00).toInt()
                binding.code.setText("$toInt")
                binding.CheckCode.text = "获取验证码"
                binding.CheckCode.isEnabled = true
            }
        })

        mValueAnimator.start()

    }

    override fun onDestroy() {
            super.onDestroy()
            mValueAnimator.cancel() //取消属性动画
        }

    }